/**
 * Created with JetBrains PhpStorm.
 * User: xuheng
 * Date: 12-9-26
 * Time: 下午12:29
 * To change this template use File | Settings | File Templates.
 */

//清空上次查选的痕迹
if ( location.hostname.indexOf( 'bigwe.com' ) > -1 ) document.domain = 'bigwe.com';
var $G = function( id ) {
	return document.getElementById( id )
};
var userLogin = function() {
	return $( '#frm_header #user_login', window.top.document ).val();
};

$( document ).on( 'click', '.synonym_text .synonym_item', function() {
	$( this ).addClass( 'selected' ).siblings().removeClass( 'selected' );
} );
/*var underlineSwicth = window.localStorage.getItem('underlineSwicth');
var $underline_switch = $('#underline_switch');
if (underlineSwicth == 0) {
	$underline_switch.removeClass('active');
}
$underline_switch.css('display', 'inline-block');*/


var findtxt = '';
//是否区分大小写
function getMatchCase( id ) {
	//return $G(id).checked ? true : false;
	return false;
}

function hideUnderlinePanel() {
	$( '#synonym', parent.document ).hide();
}

$( document ).on( 'click', '[bw-ui-switch]', function() {
	var $this = $( this );
	if ( $this.hasClass( 'active' ) ) {
		$this.removeClass( 'active' );
		window.top.localStorage.setItem( 'underlineSwicth' + userLogin(), 0 );
	} else {
		$this.addClass( 'active' );
		window.top.localStorage.removeItem( 'underlineSwicth' + userLogin() );
	}
} );

//替换
$G( "repalceBtn" ).onclick = function() {
	var obj,
		//replacetxt = $G("replacetxt").value.replace(/^\s|\s$/g, "");
		replacetxt = $( '.synonym_text .synonym_item.selected' ).text();
	if ( !findtxt ) {
		return false;
	}
	if ( findtxt == replacetxt || ( !getMatchCase( "matchCase1" ) && findtxt.toLowerCase() == replacetxt.toLowerCase() ) ) {
		return false;
	}
	obj = {
		searchStr: findtxt,
		dir: 1,
		casesensitive: getMatchCase( "matchCase1" ),
		replaceStr: replacetxt
	};
	frCommond( obj );
};
//全部替换
$G( "repalceAllBtn" ).onclick = function() {
	var obj,
		//replacetxt = $G("replacetxt").value.replace(/^\s|\s$/g, "");
		replacetxt = $( '.synonym_text .synonym_item.selected' ).text();
	if ( !findtxt ) {
		return false;
	}
	if ( findtxt == replacetxt || ( !getMatchCase( "matchCase1" ) && findtxt.toLowerCase() == replacetxt.toLowerCase() ) ) {
		return false;
	}
	obj = {
		searchStr: findtxt,
		casesensitive: getMatchCase( "matchCase1" ),
		replaceStr: replacetxt,
		all: true
	};
	var num = frCommond( obj );
	if ( num ) {
		//$G('replace-msg').innerHTML = lang.countMsg.replace("{#count}", num);
	}
};

var doquery = function( str ) {
	findtxt = $.trim( str )
	if ( findtxt == '' ) return;
	$( '.js_remove' ).remove();
	$( '.synonym_text' ).append( '<span class="gray js_remove">正在查找...</span>' );
	$( '.replace_word' ).prop( 'disabled', 'disabled' );
	
	window.top.similarWordRequest( findtxt, function(res) {
		$( '.js_remove' ).remove();
		if ( res.ret == 0 && res.data && res.data.value && res.data.value.length ) {
			$( '.replace_word' ).removeProp( 'disabled' );
			for ( var i in res.data.value ) {
				var replaceVal = res.data.value[ i ];
				$( '.synonym_text' ).append( '<span class="synonym_item js_remove ' + ( i == 0 ? 'selected' : '' ) + '">' + replaceVal + '</span>' );
			}
		} else {
			$( '.synonym_text' ).append( '<span class="gray js_remove">未找到同义词，换一个试试</span>' );
		}
	} );
}

$( '.synonyms_text' ).on( 'click', '.synonyms_replace', function() {
	$( this ).addClass( 'selected' ).siblings().removeClass( 'selected' );
} );

//执行
var frCommond = function( obj ) {
	hideUnderlinePanel();
	return window.parent.current_editor.execCommand( "searchreplace", obj );
};